<template>
  <div id="add-picture-page">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>
    <!-- 选择上传方式 -->
    <a-tabs v-model:activeKey="uploadType"
    >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlpictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
    </a-tabs>
    <a-form
      layout="vertical"
      :model="pictureForm"
      name="basic"
      @finish="handSubmit"
      autocomplete="off"
      v-if="picture"
    >
      <a-form-item name="name" label="名称">
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item name="instruction" label="简介">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :rows="2"
          autosize
          allowClear
        ></a-textarea>
      </a-form-item>
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          allowClear
          :options="categoryOptions"
        />
      </a-form-item>
      <a-form-item name="tags" label="标签">
        <a-select
          v-model:value="pictureForm.tags"
          placeholder="请输入标签"
          allowClear
          :options="tagsOptions"
          mode="tags"
        ></a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">提交</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import PictureUpload from '@/components/picture/pictureUpload.vue'
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import {
  editPictureUsingPost,
  getPictureByIdUsingGet,
  listPictureTagCategoryUsingGet,
} from '@/api/pictureController'
import { useRoute, useRouter } from 'vue-router'
import UrlpictureUpload from '@/components/picture/UrlpictureUpload.vue'

const picture = ref<API.PictureVO>()
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
}
const router = useRouter()
const pictureForm = reactive<API.PictureEditRequest>({})
const route = useRoute()
const uploadType = ref<'file' | 'url'>('file')

/**
 * 提交表单
 * @param values
 */
const handSubmit = async (values: any) => {
  const pictureId = picture.value?.id
  if (!pictureId) {
    return
  }
  const res = await editPictureUsingPost({
    id: pictureId,
    ...values,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success('图片提交成功')
    router.push({
      path: `/picture/${pictureId}`,
    })
  } else {
    message.error('图片提交失败' + res.data.message)
  }
  return
}
const categoryOptions = ref<string[]>([])
const tagsOptions = ref<string[]>([])
//获取分类和标签列表
const getCategoryTagOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    //   转换数据为下拉选项所需要的格式
    tagsOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('加载选项失败' + res.data.message)
  }
}

onMounted(() => {
  getCategoryTagOptions()
})
//编辑图片获取图片信息
const getOldPicture = async () => {
  const id = route.query?.id
  console.log(id)
  if (id) {
    const res = await getPictureByIdUsingGet({
      id
    })
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data
      picture.value=data
      pictureForm.introduction = data.introduction
      pictureForm.name = data.name
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
    else {
      message.error('获取图片失败' + res.data.message)
    }
  }
}
onMounted(()=>{
  getOldPicture()

})
</script>
<style scoped>
#add-picture-page {
  max-width: 720px;
  margin: 0 auto;
}
</style>
